package sg.edu.nus.iss.club.gui;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import sg.edu.nus.iss.club.*;
import java.awt.List;

import javax.swing.*;

public class MemberPanel extends JPanel {

	/**
	 * @param args
	 */
	
	private Button bAdd;
	private Button bRemove;
	private ClubApplication parent;
	private ArrayList <Member> members;
	private List memberList;

	
	public MemberPanel(ClubApplication parent) {
		this.parent = parent;
		
		setLayout (new BorderLayout());
        memberList = new List (5);
        memberList.setMultipleMode (false);
        add ("North", new JLabel ("Members"));
        add ("Center", memberList);
        add("East",createButtonPanel());
		
		
		
	}
	
	public void refresh() {
		members = parent.getMembers();
		memberList.removeAll();
		for (Member m : members) {
			memberList.add(m.toString());
		}
		
		
	}
	
	private MouseListener bAddlisterner = new MouseAdapter() {
		public void mouseClicked (MouseEvent e) {
			System.out.println("add button clicked");
			 AddMemberDialog d = new AddMemberDialog (parent);
              d.pack();
              d.setVisible (true);
	
		}
	};
	private MouseListener bRemovelisterner = new MouseAdapter() {
		public void mouseClicked (MouseEvent e) {
			System.out.println("remove button clicked");
			 RemoveMemberDialog d = new RemoveMemberDialog (parent,getSelectedMember() );
              d.pack();
              d.setVisible (true);
	
		}
	};
	
	private JPanel createButtonPanel() {
		JPanel buttonPanel = new JPanel();
		bAdd = new Button("Add");
		bAdd.addMouseListener(bAddlisterner);
		buttonPanel.add(bAdd);
		bRemove = new Button("Remove");
		bRemove.addMouseListener(bRemovelisterner);
		buttonPanel.add(bRemove);
		
		
		return buttonPanel;
	}
	
	
	private int getSelectedMember() {
		int idx = memberList.getSelectedIndex();
        return idx;
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
